package com.dcits.electronic_account.utils;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: CZJ
 * @Date： 2022/6/26
 */

/**
 * 时间与时间戳互相转换工具类
 */
@Service
public class DateStampUtil {
    /**
     * 将时间转换为时间戳
     */
    public static Long dateToLong(Date s) throws ParseException {
        long ts = s.getTime();
        return ts;
    }

    /**
     * 将时间戳转换为时间
     */
    public static Date longToDate(Long s){
        Date date = new Date(s);
        return date;
    }

    /**
     * Date转换成String
     */
    public static String dateToString(Date date) {
        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式
        if (date != null){
            String timeStr = null;
            try {
                timeStr = sformat.format(date);
            }catch (Exception e){
                e.printStackTrace();
            }
            return timeStr;
        }
        return null;
    }
    public static String dateToString2(Date date) {
        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");//日期格式
        if (date != null){
            String timeStr = null;
            try {
                timeStr = sformat.format(date);
            }catch (Exception e){
                e.printStackTrace();
            }
            return timeStr;
        }
        return null;
    }

    /**
     * String转换成Date
     */
    public static Date stringToDate(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式
        if (!StringUtils.isEmpty(time)){
            Date date = null;
            try {
                date = format.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
        return null;
    }
    /**
     * 转换毫秒数成“分、秒”，如“01:53”。若超过60分钟则显示“时、分、秒”，如“01:01:30
     *
     * @param待转换的毫秒数
     * */
    public static String converLongTimeToStr(long time) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;

        long hour = (time) / hh;
        long minute = (time - hour * hh) / mi;
        long second = (time - hour * hh - minute * mi) / ss;

        String strHour = hour < 10 ? "0" + hour : "" + hour;
        String strMinute = minute < 10 ? "0" + minute : "" + minute;
        String strSecond = second < 10 ? "0" + second : "" + second;

        return strHour + "时" + strMinute + "分" + strSecond + "秒";
    }

    /**
     * Func: 自定义从此时此刻开始向前或向后几天的时间，例如：-7代表向前推7天， 7代表向后推7天
     * @param days
     * @return
     */
    public static String customizeDate(Integer days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, days);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(calendar.getTime());

        return dateStr;
    }

    /**
     * Func：把String类型的时间换成Long类型的时间戳（s）
     * @param str
     * @return
     */
    public static Long stringDataToLong(String str){
        if(!StringUtils.isEmpty(str)){
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date=format.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);

            return ca.getTimeInMillis();
        }
        return null;

    }

}
